﻿// -----------------------------------------------------------------------------
// 园丁,是个很简单的管理系统
//  gitee:https://gitee.com/hgflydream/Gardener 
//  issues:https://gitee.com/hgflydream/Gardener/issues 
// -----------------------------------------------------------------------------

using Gardener.Core.Common;
using Gardener.Core.Dtos;
using Gardener.Iot.Dtos;
using Gardener.Iot.Services;
using Gardener.Weighbridge.Impl.Entities;
using Gardener.Weighbridge.Services;
using Microsoft.AspNetCore.Mvc;

namespace Gardener.Weighbridge.Impl.Services
{
    /// <summary>
    /// 地磅配置服务
    /// </summary>
    [ApiDescriptionSettings("Weighbridge", Module = "weighbridge")]
    public class WeighbridgeConfigService : ServiceBase<WeighbridgeConfig, WeighbridgeConfigDto, Guid, GardenerMultiTenantDbContextLocator>, IWeighbridgeConfigService
    {
        private readonly IDeviceService deviceService;
        private readonly IProductService productService;

        /// <summary>
        /// 地磅配置服务
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="deviceService"></param>
        /// <param name="productService"></param>
        public WeighbridgeConfigService(IRepository<WeighbridgeConfig, GardenerMultiTenantDbContextLocator> repository, IDeviceService deviceService, IProductService productService) : base(repository)
        {
            this.deviceService = deviceService;
            this.productService = productService;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <remarks>
        /// 高级查询，根据输入条件组合进行数据查询和排序
        /// </remarks>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public override async Task<PageList<WeighbridgeConfigDto>> Search(PageRequest request)
        {
            PageList<WeighbridgeConfigDto> page = await base.Search(request);
            if (page.Items.Any())
            {
                foreach (var item in page.Items)
                {
                    await FillDeviceInfo(item);
                }
            }
            return page;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <remarks>
        /// 根据主键查找单条数据，如果不存在，返回null
        /// </remarks>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("find/{id}")]
        public override async Task<WeighbridgeConfigDto?> Find(Guid id)
        {
            WeighbridgeConfigDto ? dto=await base.Find(id);
            
            return await FillDeviceInfo(dto);
        }

        private async Task<WeighbridgeConfigDto?> FillDeviceInfo(WeighbridgeConfigDto? dto)
        {
            if (dto != null)
            {
                var device = await deviceService.Find(dto.DeviceId);
                if (device != null)
                {
                    dto.OnLine = device.ConnectingDeviceConnection != null;
                    if (device.ProductId != null)
                    {
                        var product = await productService.Find(device.ProductId.Value);
                        if (product != null)
                        {
                            dto.ProductImages = System.Text.Json.JsonSerializer.Deserialize<ProductImageInfo[]>(product.ProductImages);
                        }
                    }
                }
            }
            return dto;
        }
    }
}
